package gal;

import gal.obj.Body2D;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.util.ArrayList;

import javax.swing.JPanel;

public class Points extends JPanel
{

	ArrayList<Body2D[]> timeLine = new ArrayList<Body2D[]>();
	ArrayList<Color> colors = new ArrayList<Color>();

	public Points(ArrayList<Body2D[]> timeLine)
	{
		this.timeLine = timeLine;
		colors.add(Color.red);
		colors.add(Color.blue);
		colors.add(Color.blue);
		colors.add(Color.blue);
		colors.add(Color.blue);
		colors.add(Color.blue);
		colors.add(Color.blue);
		colors.add(Color.blue);
		colors.add(Color.blue);
		colors.add(Color.blue);
		colors.add(Color.blue);
		colors.add(Color.blue);
		colors.add(Color.blue);
		colors.add(Color.blue);
		colors.add(Color.blue);
		colors.add(Color.blue);
		colors.add(Color.blue);
		colors.add(Color.blue);
		colors.add(Color.blue);
		colors.add(Color.blue);
		colors.add(Color.blue);
	}

	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		int R = 2;

		Graphics2D g2d = (Graphics2D) g;
		//setBackground(Color.BLACK);
		g2d.setColor(Color.blue);

		Dimension size = getSize();
		Insets insets = getInsets();

		int w = size.width - insets.left - insets.right;
		int h = size.height - insets.top - insets.bottom;
		Double maxW = Double.MIN_NORMAL;
		Double maxH = Double.MIN_NORMAL;
		Double minW = Double.MAX_VALUE;
		Double minH = Double.MAX_VALUE;
		// determino dimensioni massime
		for (int i = 0; i < timeLine.size(); i++)
		{
			Body2D[] u = timeLine.get(i);
			for (int j = 0; j < u.length; j++)
			{
				if (maxW < u[j].getX())
					maxW = u[j].getX();
				if (maxH < u[j].getY())
					maxH = u[j].getY();

				if (minW > u[j].getX())
					minW = u[j].getX();
				if (minH > u[j].getY())
					minH = u[j].getY();
			}
		}
		double ratioW = Math.abs(maxW - minW);
		double ratioH = Math.abs(maxH - minH);
		double ratio=Math.max(ratioH, ratioW);
		// System.out.println("maxW:"+maxW);
		// System.out.println("minW:"+minW);
		// System.out.println("maxH:"+maxH);
		// System.out.println("minH:"+minH);
		// System.out.println("------------------");
		// System.out.println("ratioW:"+ratioW);
		// System.out.println("ratioH:"+ratioH);

		g2d.setColor(Color.LIGHT_GRAY);
		g2d.drawLine(0, size.height / 2, size.height, size.height / 2);
		g2d.drawLine(size.width / 2, 0, size.width / 2, size.width);
		int step = timeLine.size()/200+1;
		for (int i = 0; i < timeLine.size(); i=i+step)
		{
			Body2D[] u = timeLine.get(i);
			for (int j = 0; j < u.length; j++)
			{
				Color c = colors.get(j % colors.size());
				g2d.setColor(c);
				int x = (int) (u[j].getX() / ratio * size.width - minW / ratio * size.width)/2+size.width/4;
				int y = (int) (u[j].getY() / ratio * size.height - minH / ratio * size.height)/2+size.height/4;
				// System.out.println(i+")  u["+j+"].getX():"+u[j].getX()+"   \t u["+j+"].getY():"+u[j].getY()+"\t \t x:"+x+"\t y:"+y);
				// g2d.drawLine(x, y, x, y);
				R = (int) (Math.max(2, Math.log10(u[j].getMass())-22));
				g2d.fillOval(x, y, R, R);
			}
		}
		System.out.println("EnD");

	}
	/*
	 * public static void main(String[] args) {
	 * 
	 * JFrame frame = new JFrame("Points");
	 * frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new
	 * Points(new ArrayList<Body2D[]>())); frame.setSize(250, 200);
	 * frame.setLocationRelativeTo(null); frame.setVisible(true); }
	 */
}
